#include "picshow_window.h"
#include "ui_picshow_window.h"
#include "main_window.h"
#include <QPixmap>
extern Main_Window *pmain_window;
Picshow_window::Picshow_window(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Picshow_window)
{
    ui->setupUi(this);
    ui->Back_menu->installEventFilter(this);
    ui->Set_menu->installEventFilter(this);
    ui->up->installEventFilter(this);
    ui->down->installEventFilter(this);
    QPixmap pax;
    pax.load(":/picture/main_window/返回菜单.png");
    ui->Back_menu->setPixmap(pax);
    ui->Back_menu->setScaledContents(true);
    pax.load(":/picture/picture_window/上一个.png");
    ui->up->setPixmap(pax);
    ui->up->setScaledContents(true);
    pax.load(":/picture/picture_window/下一个.png");
    ui->down->setPixmap(pax);
    ui->down->setScaledContents(true);
}
void Picshow_window::send_num(int num){
    myflag = num;
    show_picture();
}
void Picshow_window::show_picture(){
    QPixmap pict;

    Picture_name.append(PicturePath);
    Picture_name.append(pmain_window->Pichoose_page->fileList.at(pmain_window->Pichoose_page->now_local+myflag));
    ui->picture->setPixmap(Picture_name);
    ui->picture->setScaledContents(true);

        Picture_name = "\0";
        QPalette pe;
        pe.setColor(QPalette::WindowText,Qt::white);
        ui->Set_menu->setPalette(pe);
}
Picshow_window::~Picshow_window()
{
    delete ui;
}
void Picshow_window::paintEvent(QPaintEvent *event){
    QPainter painter(this);
    QPen pen(QColor(255,255,255));
    QFont font;
    font.setFamily("Microsoft YaHei");
    painter.setPen(pen);

    /*背景颜色*/
    painter.setBrush(QColor(75,99,147));
    painter.drawRect(this->rect());
}
bool Picshow_window::eventFilter(QObject *obj, QEvent *event)
{
    QPixmap pic;
    if (obj == ui->Back_menu)//指定某个QLabel
     {
         if (event->type() == QEvent::MouseButtonPress) //鼠标点击
         {
             QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event); // 事件转换
             if(mouseEvent->button() == Qt::LeftButton)
             {
                 this->hide();
                 pmain_window->Pichoose_page->show();
                 return true;
             }
             else
             {
                 return false;
             }
         }
         else
         {
             return false;
         }
     }
    if (obj == ui->up)//指定某个QLabel
     {
         if (event->type() == QEvent::MouseButtonPress) //鼠标点击
         {
             QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event); // 事件转换
             if(mouseEvent->button() == Qt::LeftButton)
             {
                 myflag--;
                 if(pmain_window->Pichoose_page->now_local+myflag < 0){
                     myflag = pmain_window->Pichoose_page->picture_num - pmain_window->Pichoose_page->now_local-1;
                 }
                 show_picture();
                 return true;
             }
             else
             {
                 return false;
             }
         }
         else
         {
             return false;
         }
     }
    if (obj == ui->down)//指定某个QLabel
     {
         if (event->type() == QEvent::MouseButtonPress) //鼠标点击
         {
             QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event); // 事件转换
             if(mouseEvent->button() == Qt::LeftButton)
             {
                 myflag++;
                 if(pmain_window->Pichoose_page->now_local+myflag >= pmain_window->Pichoose_page->picture_num){
                     myflag = -pmain_window->Pichoose_page->now_local;
                 }
                 show_picture();
                 return true;
             }
             else
             {
                 return false;
             }
         }
         else
         {
             return false;
         }
     }
    if (obj == ui->Set_menu)//指定某个QLabel
     {
         if (event->type() == QEvent::MouseButtonPress) //鼠标点击
         {
             QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event); // 事件转换
             if(mouseEvent->button() == Qt::LeftButton)
             {
                 pmain_window->picture_flag = pmain_window->Pichoose_page->now_local+myflag ;
                // qDebug()<<pmain_window->picture_flag ;
                 QMessageBox::information(this,"信息提示","设置成功");
                 return true;
             }
             else
             {
                 return false;
             }
         }
         else
         {
             return false;
         }
     }
}
